package controller

import (
	"log"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

var Websocket = websocketController{}

type websocketController struct {
}

func (w *websocketController) Index(r *ghttp.Request) {

	//view := g.View()

	r.Response.WriteTpl("wechat/index.tpl", g.Map{
		"title": "websocket",
	})

}

func (w *websocketController) Server(r *ghttp.Request) {

	conn, err := upgrader.Upgrade(r.Response.Writer, r.Request, nil)

	if err != nil {
		log.Print("upgrade:", err)
		return
	}
	for {
		//接受消息
		var receive []byte
		messageType, receive, err := conn.ReadMessage()
		if err != nil {
			log.Println(err)
			return
		}

		message <- &Message{
			conn:        conn,
			content:     receive,
			context:     r.Request.Context(),
			messageType: messageType,
		}
	}
}
